<?php //pack11class

class GetQuery {

    const LETTERS="ABCDEFGHIJKLMOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    private static $uid;
    private static $firstRun=true;
    public static $qline="";

    public static function generate(GetQueryObject $queryObject) {
        $query="?get=";
        $query="";
        $propertiesArray=array();
        $settingsArray=array();
        $qline="Properties=>[";
        foreach($queryObject->properties as $key=>$value) {
            $cp=PropertiesManager::getProperty($key);
            if($cp) {
                if(is_numeric($cp->value)) $qline.=$cp->system.":".$cp->value."; ";
                if(is_numeric($value)) array_push($propertiesArray,$key.":".$value);
            } else {
                trace("Wrong property key {$key}");
            }
        }

        $qline.="] Settings=>[";

        foreach($queryObject->settings as $key=>$value) {
            $cs=SettingsManager::getSetting($key);
            if($cs) {
                if(is_numeric($cs->value)) $qline.=$cs->system.":".$cs->value."; ";
                if(is_numeric($value)) array_push($settingsArray,$key.":".$value);
            } else {
                trace("Wrong setting key {$key}");
            }
        }

        $qline.="]";

        self::$qline=$qline;

        //trace($qline);

        shuffle($propertiesArray);
        shuffle($settingsArray);

        //add properties
        foreach($propertiesArray as $value) {
            $v=explode(":",$value);
            $l1=self::getLetter();
            $l2=self::getLetter();
            $query.=($v[0]+self::getExtraNumber($l1)).$l1.($v[1]+self::getExtraNumber($l2)).$l2;
            //trace("AddedProperty: ".$query);
        }

        if(count($settingsArray)>0) {
            $query.="N";

            foreach($settingsArray as $value) {
                $v=explode(":",$value);
                $l1=self::getLetter();
                $l2=self::getLetter();
                $query.=($v[0]+self::getExtraNumber($l1)).$l1.($v[1]+self::getExtraNumber($l2)).$l2;
                //trace("AddedSetting: ".$query);
            }
        }

        $splitter=rand(0,32);
        $uid=MD5($query);
        return substr($uid,0,$splitter).$query.substr($uid,$splitter);
    }

    private static function getLetter() {
        $separator="";
        for($i=0; $i<rand(1,3);$i++) {
            $separator.=substr(self::LETTERS,rand(0, strlen(self::LETTERS)-1),1);
        }
        return $separator;
    }

    public static function translate($s="test") {
        //trace("TRANSLATOR ".((self::$firstRun)? "true":"false"));
        $translateResult="";
        array_push(Tracer::$traceFrom,"GetQuery");
        if(!isset($_GET["state"])) return;
        $query=(self::$firstRun)? $_GET["state"]:$s;
        $queryObject = new GetQueryObject;
        $count=0;
        $str=$substr="";
        while(MD5($str)!=$substr) {
            $str=substr($query,$count,strlen($query)-32);
            $substr=str_replace($str,"",$query);
            $count++;
            if(abs($count)>strlen($query)) {
                break;
                return false;
            }
        }
        $query=$str;
        $arrays=explode("N",$str);
        //trace("Str: ".$str);
        //print_r($arrays);
        for($i=0; $i<count($arrays); $i++) {

            $query=$arrays[$i];
            $numArray = preg_split('/([0-9]){1,}/', $query,-1,PREG_SPLIT_NO_EMPTY);
            $queryArray = preg_split('/([a-zA-Z]){1,}/', $query,-1,PREG_SPLIT_NO_EMPTY);



            while(current($queryArray)) {
                $num=current($queryArray)-self::getExtraNumber($numArray[key($queryArray)]);
                $real=current($queryArray);
                $val=next($queryArray)-self::getExtraNumber($numArray[key($queryArray)]);

                switch($i){
                    case 0: $prop=PropertiesManager::getProperty($num);
                        break;

                    case 1: $prop=SettingsManager::getSetting($num);
                        break;
                }
                //trace(count($arrays)." ".$i." ".$prop);

                if(self::$firstRun) {
                    if(get_class($prop)=="Setting") {
                        SettingsManager::setSetting($prop->id, $val);
                        //trace($prop->system." ".$prop->value);
                    } else {
                        $prop->value=$val;
                    }
                } else {
                    $translateResult.="     ".$prop->system."=".$val;
                }

            //trace($prop);

                $num=next($queryArray);
            }
        }
            //if(!self::$firstRun) trace($s."=>".$translateResult);
            self::$firstRun=false;
            return true;
    }

    private static function removeEmptyItems($var) {
        return ($var!="");
    }

    private static function getExtraNumber($s) {
        $f=substr($s,0,1);
        return strpos(self::LETTERS,$f);
    }

    public static function useDefinedVars() {
        $queryObject=new GetQueryObject;
        foreach(PropertiesManager::properties() as $property) {
            if(is_numeric($property->value)) {
                //trace($property);
                $queryObject->setValue($property);
            }
        }

        foreach(SettingsManager::settings() as $setting) {
            if(is_numeric($setting->value)) {
                //trace($setting);
                $queryObject->setValue($setting,GetQueryObject::SETTING);
            }
        }

        return $queryObject;
    }

    /*public static function getDataFromPOST() {
        foreach($_POST as $key=>$value) {
            $property=PropertiesManager::getProperty($key);
            if($property) $queryObject->setValue($property);
        }
    }*/

}
?>
